package com.xinke.module.system.service.oauth2;

import cn.hutool.core.util.IdUtil;
import com.xinke.component.common.util.sno.IdGen;
import com.xinke.module.system.model.entity.oauth2.OAuth2ClientDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import com.xinke.module.system.model.vo.oauth2.*;
import com.xinke.module.system.model.entity.oauth2.OAuth2RefreshTokenDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.oauth2.OAuth2RefreshTokenMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;

/**
 * OAuth2 刷新令牌 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class OAuth2RefreshTokenServiceImpl implements OAuth2RefreshTokenService {

    @Resource
    private OAuth2RefreshTokenMapper oAuth2RefreshTokenMapper;

    @Override
    public Long createOAuth2RefreshToken(OAuth2RefreshTokenSaveReqVO createReqVO) {
        // 插入
        OAuth2RefreshTokenDO oAuth2RefreshToken = BeanUtils.toBean(createReqVO, OAuth2RefreshTokenDO.class);
        oAuth2RefreshTokenMapper.insert(oAuth2RefreshToken);
        // 返回
        return oAuth2RefreshToken.getRefreshId();
    }

    @Override
    public OAuth2RefreshTokenDO createOAuth2RefreshToken(Long userId, Integer userType, OAuth2ClientDO clientDO, List<String> scopes) {
        OAuth2RefreshTokenDO refreshToken = new OAuth2RefreshTokenDO().setRefreshToken(generateRefreshToken())
                .setUserId(userId).setUserType(userType)
                .setClientNo(clientDO.getClientNo()).setAuthScope(scopes)
                .setExpiresTime(LocalDateTime.now().plusSeconds(clientDO.getRefreshTokenValiditySeconds()));
        refreshToken.setRefreshId(IdGen.nextId());
        oAuth2RefreshTokenMapper.insert(refreshToken);
        return refreshToken;
    }

    @Override
    public void updateOAuth2RefreshToken(OAuth2RefreshTokenSaveReqVO updateReqVO) {
        // 校验存在
        validateOAuth2RefreshTokenExists(updateReqVO.getRefreshId());
        // 更新
        OAuth2RefreshTokenDO updateObj = BeanUtils.toBean(updateReqVO, OAuth2RefreshTokenDO.class);
        oAuth2RefreshTokenMapper.updateById(updateObj);
    }

    @Override
    public void deleteOAuth2RefreshToken(Long refreshId) {
        // 校验存在
        validateOAuth2RefreshTokenExists(refreshId);
        // 删除
        oAuth2RefreshTokenMapper.deleteById(refreshId);
    }

    private void validateOAuth2RefreshTokenExists(Long refreshId) {
        if (oAuth2RefreshTokenMapper.selectById(refreshId) == null) {
            throw exception(null);
        }
    }

    @Override
    public OAuth2RefreshTokenDO getOAuth2RefreshToken(Long refreshId) {
        return oAuth2RefreshTokenMapper.selectById(refreshId);
    }

    @Override
    public PageResult<OAuth2RefreshTokenDO> getOAuth2RefreshTokenPage(OAuth2RefreshTokenPageReqVO pageReqVO) {
        return oAuth2RefreshTokenMapper.selectPage(pageReqVO);
    }

    private static String generateRefreshToken() {
        return IdUtil.fastSimpleUUID();
    }

}